package com.xiaoxu.privatechat.dao.home;


import com.xiaoxu.privatechat.entity.common.Account;
import com.xiaoxu.privatechat.entity.home.FriendRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description 好友请求数据库处理
 * @date 2023/2/13
 * @author xiaoxu
 */
@Repository
public interface FriendRequestDao extends JpaRepository<FriendRequest, Long> {

	/**
	 * @description 根据好友请求id查询请求
	 * @param id 请求id
	 * @return FriendRequest
	 */
	@Query("select fr from FriendRequest fr where fr.id = :id")
	FriendRequest find(@Param("id")Long id);

	/**
	 * @description 根据发送方和接收方查询请求
	 * @param sender 发送方
	 * @param reciever 接收方
	 * @return FriendRequest
	 */
	FriendRequest findBySenderAndReciever(Account sender,Account reciever);

	/**
	 * @description 通过接收方方和请求时间查询好友请求
	 * @param reciever 接收方
	 * @return List<FriendRequest>
	 */
	List<FriendRequest> findByRecieverOrderByUpdateTimeDesc(Account reciever);

	/**
	 * @description 获取请求数
	 * @param recieverId 接收方id
	 * @param status 状态
	 * @return Integer
	 */
	@Query("select count(fr.id) from FriendRequest fr where fr.reciever.id = :recieverId and fr.status = :status")
	Integer getFriendRequestCount(@Param("recieverId")Long recieverId,@Param("status")int status);
}
